﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using snat.algorithms;
using System;

namespace gui {
    /// <summary>
    /// Interaction logic for ConnectBox.xaml
    /// </summary>
    public partial class AlgorithmsSelectBox : Window {

        private ObservableCollection<AlgorithmItem> AlgorithmsList {
            get { return AlgorithmSelectListInfo.Instance.AlgorithmsList; }
            set { AlgorithmSelectListInfo.Instance.AlgorithmsList = value; }
        }
        public Dictionary<string, AlgorithmItem> SelectedList { get; private set; }
        public static AlgorithmItem SelectedAlgorithm;
        public bool AllowMutliSelect { get; private set; }

        public AlgorithmsSelectBox(Dictionary<string, IAlgorithm> list, bool allowMultipleSelection) {
            AllowMutliSelect = allowMultipleSelection;
            SelectedList = new Dictionary<string, AlgorithmItem>();
            InitializeComponent();

            // Setup List Binding
            AlgorithmSelectListInfo._Dispatcher = this.Dispatcher;
            this.DataContext = AlgorithmSelectListInfo.Instance;

            // Populate Algorithms List
            bool first = true;
            AlgorithmsList = new ObservableCollection<AlgorithmItem>();
            foreach (KeyValuePair<String, IAlgorithm> a in list) {
                if (first) {
                    AlgorithmsList.Add(new AlgorithmItem(a.Value, a.Key, true));
                    SelectedAlgorithm = AlgorithmsList[0];
                    first = false;
                } else {
                    AlgorithmsList.Add(new AlgorithmItem(a.Value, a.Key, false));
                }
            }

            // If multiple algorithm selection is not allow, grey out the select all/none buttons
            if (!allowMultipleSelection) {
                SelectAllXAML.IsEnabled = false;
                SelectNoneXAML.IsEnabled = false;
            }

            // Make this window the topmost window
            this.Topmost = true;
        }

        // On OK button clicked
        private void OK(object sender, RoutedEventArgs e) {
            if (AlgorithmsList.Count > 0) {

                // If something in the list was selected
                if (AllowMutliSelect) {
                    if (SelectedList.Count > 0) this.Close();
                    else {
                        // Nothing selected so show a warning box
                        MessageBox.Show("You have not selected anything!", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                } else {

                    // Else we are dealing with single-selection, so clear the list and add in the selected
                    SelectedList.Clear();
                    SelectedList.Add(SelectedAlgorithm.Name, SelectedAlgorithm);
                    this.Close();
                }
            } else {
                MessageBox.Show("Please register an appropriate algorithm.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                this.Close();
            }
        }

        private void SelectAll(object sender, RoutedEventArgs e) {
            SelectedList.Clear();
            foreach (AlgorithmItem a in AlgorithmsList) {
                a.Selected = true;
                SelectedList.Add(a.Name, a);
            }
        }

        private void SelectNone(object sender, RoutedEventArgs e) {
            SelectedList.Clear();
            foreach (AlgorithmItem a in AlgorithmsList) a.Selected = false;
        }


        // If Multi Select is not allowed, enforce only single selection
        private void CheckBox_Checked(object sender, RoutedEventArgs e) {
            if (!AllowMutliSelect) {

                // Find newly selected
                foreach (AlgorithmItem a in AlgorithmsList) {
                    if (a.Name != SelectedAlgorithm.Name && a.Selected == true) {
                        SelectedAlgorithm = a;
                        break;
                    }
                }
                // Clear any other selected
                foreach (AlgorithmItem a in AlgorithmsList) {
                    if (a.Name != SelectedAlgorithm.Name && a.Selected == true) a.Selected = false;
                }

            } else {
                // Find and add the element just selected to the selected list
                foreach (AlgorithmItem a in AlgorithmsList) {
                    if (!SelectedList.ContainsKey(a.Name) && a.Selected == true) {
                        SelectedList.Add(a.Name, a);
                        break;
                    }
                }
            }
        }
        private void CheckBox_Unchecked(object sender, RoutedEventArgs e) {
            if (!AllowMutliSelect) {

                // Find the element just unselected, and select it again
                SelectedAlgorithm.Selected = true;

            } else {

                // Find the element just unselect, and remove it from selectedList
                foreach (AlgorithmItem a in AlgorithmsList) {
                    if (SelectedList.ContainsKey(a.Name) && a.Selected == false) {
                        SelectedList.Remove(a.Name);
                        break;
                    }
                }
            }
        }





    }
}
